﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SIPO.master" AutoEventWireup="false"
    CodeFile="recSanctionlist.aspx.cs" Inherits="SIPO.Web.Lists.Transaction.recSanctionlist" %>

<asp:Content ID="ctnBody" ContentPlaceHolderID="body" runat="server">
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>
    <div>       
        <button id="btnAdd" type="button" onclick="OpenForm('add');">
            Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">
            Ubah</button>
        <button id="btnDelete" type="button" onclick="OpenForm('delete');">
            Hapus</button>
        <button id="btnReview" type="button" onclick="OpenForm('review');">
            Review</button>
        <button id="btnApprove" type="button" onclick="OpenForm('approve');">
            Setujui</button>
        <button id="btnOpen" type="button" onclick="OpenForm('open');">
            Buka</button>
    </div>
    <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog" title="Default dialog">
    </div>
    <script type="text/javascript">
        $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
        $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
        $("#btnDelete").button({ icons: { primary: "ui-icon-trash"} });
        $("#btnReview").button({ icons: { primary: "ui-icon-pencil"} });
        $("#btnApprove").button({ icons: { primary: "ui-icon-locked"} });
        $("#btnOpen").button({ icons: { primary: "ui-icon-unlocked"} });

    </script>
    <script type="text/javascript">

        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recSanctionhandler.ashx") %>';

        $('#table').jqGrid({
            url: handlerUrl + '?method=getdata',
            datatype: 'json',
            colNames: ['ID', 'Nama Perusahaan', 'Sub Sub Sektor', 'Tipe Sanksi', 'Jumlah (Rp)', 'No. Surat Sanksi', 'Tgl. Surat Sanksi','Tgl. Jatuh Tempo', 'Tgl Review', 'Tgl Persetujuan', 'No. Surat Setoran',  'Sektor', 'Sub Sektor','Download'],
            colModel: [
                    { name: 'SanctionID', hidden: true, index: 'SanctionID', width: 60 },
                    { name: 'recDebtorSector.recDebtor.DebtorName', index: 'recDebtor.DebtorName', width: 350 },
                    { name: 'recDebtorSector.recSubSubSector.SubSubSectorName', index: 'recSubSubSector.SubSubSectorName', width: 360 },
                    { name: 'recSanctionType.SanctionTypeName', index: 'SanctionTypeName', align: 'left', width: 250 },
                    { name: 'Value', index: 'Value', formatter: 'number', align: 'right', width: 370 },
                    { name: 'LetterNo', index: 'LetterNo', align: 'left', width: 330 },
                    { name: 'LetterDate', index: 'LetterDate', align: 'center', width: 330, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                    { name: 'recBilling.DueDate', index: 'recBilling.DueDate', align: 'center', width: 330, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                    { name: 'ReviewDate', IsReview: 'ReviewDate', align: 'center', width: 225, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                    { name: 'ApproveDate', index: 'ApproveDate', align: 'center', width: 225, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                    { name: 'InvoiceNo', index: 'InvoiceNo', align: 'left', width: 330 },
                    { name: 'recDebtorSector.recSector.SectorName', index: 'recSector.SectorName', width: 300 },
                    { name: 'recDebtorSector.recSubSector.SubSectorName', index: 'recSubSector.SubSectorName', width: 350 },
                    { name: 'SanctionID', index: 'SanctionID', align: 'center', formatter: returnMyLink, width: 200 },
           	],
            rowNum: 10,
            id: 'SanctionID',
            multiselect: true,
            rowList: [5, 10, 15, 20],
            pager: '#pager',
            sortname: 'recSanction.IsReview',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 1270,
            height: '100%',
            caption: 'Sanksi',
            ajaxGridOptions: { cache: false },
            ondblClickRow: function () {
                OpenForm('edit');
            }
        });

        $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true });


        //show hide coloumn berdasar debtor login
        showhidecoloumn();

        function downloadfile(cellValue) {
                        var objrecSanction = new Object();
                        objrecSanction.SanctionID = encodeURIComponent(cellValue);
                        jsonData = JSON.stringify(objrecSanction);
                        var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx") %>';
                        $.ajax({
                            type: 'POST',
                            url: handlerUrl + "?method=checkfile",
                            contentType: 'application/json',
                            dataType: 'json',
                            data: jsonData,
                            success: function (data) {
                                if (data.IsSuccess) {
                                    var win = window.open(handlerUrl + '?method=download&sanction_id=' + cellValue, '_blank');
                                    win.focus();
                                }
                                else {
                                    alert(data.Message);
                                }
                            },
                            error: function (response) {
                                //var r = JSON.parse(response.responseText);
                                alert("Error Check File Download"); //Message: " + r.Message);
                            }
                        });

        }
        
        function returnMyLink(cellValue, options, rowdata) {
            var handleurlDownload = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx?method=download") %>';
            //return "<a target='_blank' href='" + handleurlDownload + "&sanction_id=" + cellValue + "' >download</a>";
            return "<a  href='#' onclick='downloadfile("+cellValue+")' >download</a>";
        }

        function OpenForm(ctl) {

            var formUrl = '<%= ResolveUrl("~/forms/transaction/recSanctionform.aspx") %>';
            var grid = $('#table');
            var riviewdate = "";
            var approvedate = "";
            var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
            $.each(selIds, function () {
                selText.push(grid.jqGrid('getCell', this, 'SanctionID'));
                riviewdate = grid.jqGrid('getCell', this, 'ReviewDate');
                approvedate = grid.jqGrid('getCell', this, 'ApproveDate');
            });

            if (ctl == 'edit') {

                if (selIds.length == 0) {
                    alert('Pilih data yang mau di ubah !');
                }
                else if (selIds.length > 1) {
                    alert('Pilih satu record untuk ubah data.');
                }
                else {
                    $('#dialog').load(formUrl + '?id=' + selText+'&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Ubah Sanksi' });
                    $("#dialog").dialog('open');
                }
            }
            else if (ctl == 'open') {

                if (selIds.length == 0) {
                    alert('Pilih data yang mau di buka !');
                }
                else if (selIds.length > 1) {
                    alert('Pilih satu record untuk buka data.');
                }
                else {
                    
                    if (approvedate.length == 1 && riviewdate.length==1) {
                        alert("Data tidak bisa di buka, karena belum direview dan disetujui!");
                    }
                    else {
                        var formUrlOpen = '<%= ResolveUrl("~/forms/transaction/recSanctionOpenform.aspx") %>';
                        $('#dialog').load(formUrlOpen + '?id=' + selText + '&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Buka Sanksi' });
                        $("#dialog").dialog('open');
                    }

                }
            }
            else if (ctl == 'add') {
                $('#dialog').load(formUrl+'?menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Tambah Sanksi' });
                $("#dialog").dialog('open');
            }
            else if (ctl == 'review') {

                if (selIds.length == 0) {
                    alert('Pilih data yang mau di Review !');

                }
                else if (selIds.length > 1) {
                    alert('Pilih satu record untuk Review data.');
                }
                else {
                    var formUrlReview = '<%= ResolveUrl("~/forms/transaction/recSanctionReviewform.aspx") %>';
                    $('#dialog').load(formUrlReview + '?id=' + selText + '&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Review Sanksi' });
                    $("#dialog").dialog('open');

                    
                }
            }
            else if (ctl == 'approve') {

                if (selIds.length == 0) {
                    alert('Pilih data yang mau di setujui !');
                }
                else if (selIds.length > 1) {
                    alert('Pilih satu record untuk setujui data !');
                }
                else {

                    if (riviewdate.length==1) {
                        alert("Data tidak bisa disetujui kerena belum direview !");
                    }
                    else {
                        var formUrlApprove = '<%= ResolveUrl("~/forms/transaction/recSanctionApproveform.aspx") %>';
                        $('#dialog').load(formUrlApprove + '?id=' + selText + '&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Setujui Sanksi' });
                        $("#dialog").dialog('open');
                    }

                }
            }
            else {
                if (selIds.length == 0)
                    alert('Pilih data yang mau di hapus !');
                else {

                    for (var i = 0; i < riviewdate.length; i = i + 1) {
                        if (riviewdate[i].trim().length>0) {
                            alert("Data tidak bisa di hapus karena sudah di Review !");
                            return false;
                        }
                    }

                        var result = window.confirm('Benar data akan di hapus ?');
                        if (result) {

                            var objrecSanction = new Object();
                            objrecSanction.SanctionID = encodeURIComponent(selText);
                            jsonData = JSON.stringify(objrecSanction);
                            CallAjax('<%= ResolveUrl("~/ajaxhandler/transaction/recSanctionhandler.ashx") %>', "?method=delete", jsonData);
                            setTimeout("$('#table').trigger('reloadGrid');", 3000);
                        }
                }
            }

        }

        function showhidecoloumn() {
            var debtor = "<%= base.DebtorID %>";
            if (debtor == "") {
                $("#table").jqGrid('showCol', ["ReviewDate"]);
                $("#table").jqGrid('showCol', ["ApproveDate"]);
            }
            else {
                $("#table").jqGrid('hideCol', ["ReviewDate"]);
                $("#table").jqGrid('hideCol', ["ApproveDate"]);
            }
        
        }
    
    </script>
</asp:Content>
